//!@author denglibin
//!@date 2025/07/12 10:45
//!@desc 天府融媒数据处理

use futures::executor::block_on;
use log::info;
use my_util_lib::html2text::remove_html_tags;
use my_util_lib::{date, html2text, tokio_file};
use serde::{Deserialize, Serialize};
use std::env;
use tokio::fs::File;
use tokio::io::AsyncBufReadExt;

use crate::dao::mysql::mysql_dao;
use crate::entity::content::Content;
use crate::sys::global::{to_global_result, GlobalError, GlobalResult};

#[derive(Serialize, Deserialize)]
struct DataRegion {
    pub note: Option<String>,
    pub name_: String,
}
#[derive(Serialize, Deserialize)]
struct DataObj {
    pub dataregion: DataRegion,
}

pub async fn json2sql() -> GlobalResult<()> {
    let mut json_path = r"D:\yiscn\川报\0807-sc_senti.SH_D_ENTITYDATA.json";
    let mut mysql_url = "mysql://root:denglibin@192.168.6.9:3308/denglibin";
    let mut start_id = 1;
    let args = env::args().collect::<Vec<String>>();
    if args.len() > 1 {
        let p = args.get(1).unwrap();
        json_path = p;
    }
    if args.len() > 2 {
        let p = args.get(2).unwrap();
        mysql_url = p;
    }
    if args.len() > 3 {
        let p = args.get(3).unwrap();
        start_id = p.parse::<i64>().unwrap();
    }


    info!("file_path:{}", json_path);
    info!("mysql_url:{}", mysql_url);
    info!("start_id:{}", start_id);



    let mut objs: Vec<String> = Vec::new();
    let mut obj = String::new();

    let file = tokio::fs::File::open(json_path).await?;

    let mut count = 0;
    // 将file转换为BufReader
    let mut buf_reader = tokio::io::BufReader::new(file).lines();
    let mut id: i64 = 1;

    //let mut file = File::create("demo.json").await?;

    let mut img = 0;

    // 每次读取一行
    while let Some(line) = buf_reader.next_line().await.unwrap() {
        obj.push_str(&line);
        obj.push_str("\n");
        if line.starts_with("}") && line.trim().len() == 1 {
            objs.push(obj.clone());

            //file.write_all(obj.as_bytes()).await?;
            //file.flush().await?;
            obj.clear();

            if objs.len() == 1000 {
                count += objs.len();
                let img_count = do_insert(&objs, &mut id, mysql_url, start_id).await;
                img += img_count;
                objs.clear();
                println!("id: {}--{}", id, date::get_chrono_local_now_time_str());
            }
        }
    }
    if !objs.is_empty() {
        let img_count = do_insert(&objs, &mut id, mysql_url, start_id).await;
        img += img_count;
        objs.clear();
    }

    println!("完成,数量:{}", id-1);
    println!("图片数量:{}",img);
    Ok(())
}

async fn do_insert(objs: &Vec<String>, id: &mut i64, url: &str, start_id: i64)->usize {
    let mut contents = vec![];
    let mut img_count = 0;
    for json in objs.iter() {
        let v = serde_json::from_str::<DataObj>(json);
        if v.is_err() {
            println!("错误数据:{}", json);
        }

        let DataObj { dataregion } = v.unwrap();
        let DataRegion { note, name_ } = dataregion;
        let content = if let Some(s) = note { s } else { "".into() };

        let count = content.matches("img").count();
        img_count += count;
        //先处理这个图
        // if content.contains("https://storagep9110.sctvcloud.com"){
        //     continue
        // }

        let content = html2text::remove_html_tags(&content);
        if content.is_empty() {
            continue;
        }
        let content = Content {
            id: *id,
            title: name_,
            content,
        };

        *id += 1;
        if content.id < start_id {
            continue;
        }
        contents.push(content);
    }
    let pool = mysql_dao::get_conn_pool(url, 1).await.unwrap();
    mysql_dao::insert_batch::<Content>(&contents, &pool)
        .await
        .unwrap();

    img_count
}
